#include <algorithm>
#include <memory>
#include <iostream>

using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;

class Point
{
public:
    Point(int ix, int iy)
    : _ix(ix)
    , _iy(iy)
    {   cout << "Point(int,int)" << endl;   }

    ~Point()
    {   cout << "~Point()  _ix: " 
             << _ix << ", _iy:" << _iy << endl; }

    void print() const
    {   
        cout << "(" << _ix
             << "," << _iy
             << ")" << endl;
    }

    //成员函数的返回值是一个Point的指针
    Point * addPoint(const Point & rhs)
    {
        _ix += rhs._ix;
        _iy += rhs._iy;
        return this;
    }

private:
    int _ix;
    int _iy;
};

void test0()
{
    Point * pt = new Point(1, 2);
    unique_ptr<Point> up1(pt);
    unique_ptr<Point> up2(pt);
}

void test1()
{
    cout << "test1()  shared_ptr" << endl;
    Point * pt = new Point(1, 2);
    shared_ptr<Point> sp1(pt);
    /* shared_ptr<Point> sp2(pt); */  //error
    shared_ptr<Point> sp2 = sp1;//ok
}

void test2()
{
    Point * pt = new Point(1, 2);
    Point * pt2 = new Point(10, 20);
    shared_ptr<Point> sp1(pt);

    shared_ptr<Point> sp2(pt2);
    //重置sp2托管的对象
    sp2.reset(sp1.get());
}

void test3()
{
    cout << "test3() shared_ptr" << endl;
    shared_ptr<Point> sp1(new Point(1, 2));
    shared_ptr<Point> sp2(new Point(10, 20));
    //用新的智能指针sp3托管addPoint函数返回的指针
    shared_ptr<Point> sp3(sp1->addPoint(*sp2));

}

int main()
{
    /* test0(); */
    /* test1(); */
    /* test2(); */
    test3();

    return 0;
}

